<?php
/* 
* @Author: Jerry-chen
* @Date:   2014-07-15 14:59:54
* @Last Modified time: 2014-07-28 10:12:06
* @Last Modified by: Jerry-chen<uranusbill@163.com>
* @ Copyright (c) 2014 http://www.cooldreamer.com All rightsreserved.
*/

class ListControl extends PublicControl
{
	
	public function index()
	{
		$cateData = K('Category')->get_one(array('cateid'=>Q('get.cateid')),array('pid','cateid'));
		//GET筛选
		$this->get_choose();
		
		if($cateData['pid']==0){
			//************************************筛选条件*******************************************
			//根据分类，查出类型
			//先确认，传入的CATEID的PID是否为0，就直接查找类型相关数据，如果不为0，就找父级-》继承
				$cateData = K('Category')->get_one(array('cateid'=>Q('get.cateid')));
					// P($cateData);
				$catetypeid = $cateData['catetypeid'];
				$attrData = K('Attr')->get_all(array('typeid'=>$catetypeid));
				// p($attrData);
				//取出时间,类型
				$cityData = array();
				$supportData = array();
				//循环重组数组
				foreach ($attrData as $k => $v) {
					// 取出时间
					if($v['is_type']==3){
						//字符转数组
						$v['attrvalue'] = explode(',', $v['attrvalue']);
						$cityData = $v['attrvalue'];
						
					}
					//取出支持
					if($v['is_type']==2){
						//字符转数组
						$v['attrvalue'] = explode(',', $v['attrvalue']);
						$supportData = $v['attrvalue'];
					}
				}
				
				//分配数据
				$this->assign('cityData',$cityData);

	// //********************************GET传参筛选*********************************************
	// 	$_GET['city']=isset($_GET['city'])?$_GET['city']:null;
	// 	//查出商品
	// 	  $goodsData = k('Goods')->get_all(array('cateid'=>$_GET['cateid']),'goodsid desc');
	// 	  p($goodsData);
			
//****************************************************************************************************
//========================================商品数据<首页的多少元起>++++++++++++++++++++++++++++++++++++
        //对数组重组，取需要的数据
        $goodsData = k('Goods')->get_all(null,'goodsid desc');
        //+++++++++++++++++++取出最小的价格++++++++++++++++++++++++//
        $i= -1;
        foreach ($goodsData as $key => $value) 
        {
        	$i=$i+1;
            // 找出相应的价格
           $goodstimeid =  K('Goodstime')->get_one(array('goodsid'=>$value['goodsid']),'timeid'); 
           //取出所有的价格。
           $price = K('Goodsprice')->get_all(array('goodstimeid'=>$goodstimeid));
           // 取出数组中PRICE最小的数
           foreach ($price as $km => $vm) {
              if($km==0){
                //第一次循环，先把VM值付给MIN
                $min = $vm['price'];
              }
              if($vm['price']<$min){
                $min = $vm['price'];
              }
           }
            //+++++++++++++++++++取出图片的路径++++++++++++++++++++++++//
            // 通过GOODSID查出商品的GOODSDES
            $goodsimage = K('Goodsdes')->get_one(array('goodsid'=>$value['goodsid']),'goodsimage');
            // p($goodsimage);
            // 将得到的最小价格和图片路径符值给GOODSDATA数组，好分配;
           $goodsData[$key]['minprice']=$min;
           $goodsData[$key]['goodsimage']=$goodsimage['goodsimage'];
           //序号
           $goodsData[$key]['sequenceNumber'] =  $i;

        }
        // P($goodsData);
        $threegoodsData = array_slice($goodsData, 1,3,true);
    //分配数据
       $this->assign('goodsData',$goodsData);
       $this->assign('threegoodsData',$threegoodsData);
	//========================================商品数据++++++++++++++++++++++++++++++++++++
			 $cateData = K('Category')->get_all(array('pid'=>Q('get.cateid')),'cateid');
	         // p($cateData);
	         $cateid = array();
	         if(is_array($cateData)){
	         	foreach ($cateData as $k => $v) {
	          //根据找出的CATEID查出商品数据
	          // p($v);
	         	 $cateid[]=$v['cateid'];
	          // p($cateid);
	       	  }
	         }else{
	       	  	//如果不是数组，就代表没有数据，直接载人模板后DIE掉
	       	  	$this->display('list.html');
	       	  	die;
	       	  }
	         
	         //-----------------------
	         // p($cateid);
	          // $allCtaeData = K('GoodsView')->get_all(null,array('cateid'=>$cateid),null);
	          // p($allCtaeData);
	         // die;
	         // -------------------------
	         $allCtaeData = K('Goods')->get_in(array('cateid'=>$cateid),'goodsid desc',array('goodsid','title','cinemaid','startime','overtime','goodsname'));
	         // p($allCtaeData);
	         //商品数据---
	         $goodsid=array();
	        foreach ($allCtaeData as $key => $value) 
	        {
	          $goodsid[]=$value['goodsid'];
	        }
	        $cinemaid = array();
	        foreach ($allCtaeData as $key => $value) {
	        	$cinemaid[] =$value['cinemaid'];
	        }
	        //时间数据
	        // $timeData = K('Goodstime')->get_in(array('goodsid'=>$goodsid),'goodsid desc',array('timeid','timename','goodsid'));
	        //查出
	        // p($timeData);
	        //DED详细描述---
	         $desData = K('Goodsdes')->get_in(array('goodsid'=>$goodsid),'goodsid desc',array('goodsimage'));
	         
	         // 数组的组合---图片路径/地点
	        foreach ($allCtaeData as $k => $v) {
	          // p($desData[$k]['goodsimage']);
	          $allCtaeData[$k]['goodsimage'] = $desData[$k]['goodsimage'];
	          $place = K('Cinema')->get_one(array('cinemaid'=>$v['cinemaid']));
	          // p($place);
	          $allCtaeData[$k]['cinemaname'] = $place['cinemaname'];
	          $allCtaeData[$k]['cinemaaddress'] = $place['cinemaaddress'];
	           // 城市
	   		 $city = k('Goodsattr')->get_one(array('goodsid'=>$v['goodsid']),array('city','goodsid'));
	   		 // p($city);
	          $allCtaeData[$k]['city'] = $city['city'];
	    	  }
	    	  // p($cityData);
	    	  //******************GET传参数判断顶级导航****************
	    	  $_GET['city'] = isset($_GET['city'])?$_GET['city']:NULL;
	    	  $temarr = array();
	    	  if($_GET['city'])
	    	  {
	    	  	foreach ($allCtaeData as $k => $v) {
	    	  		if($v['city']==$_GET['city']){
	    	  			$temarr[] = $allCtaeData[$k];
	    	  		}
	    	  	}
	    	  	// p($temarr);
	    	  	$allCtaeData = $temarr;
	    	  }
	    	  // p($allCtaeData);
	    	  $this->assign('allCtaeData',$allCtaeData);
	        }else{
	        	//************************************筛选条件*******************************************
				//根据分类，查出类型
				//先确认，传入的CATEID的PID是否为0，就直接查找类型相关数据，如果不为0，就找父级-》继承
				$cateData = K('Category')->get_one(array('cateid'=>$cateData['pid']));
					// P($cateData);
				$catetypeid = $cateData['catetypeid'];
				$attrData = K('Attr')->get_all(array('typeid'=>$catetypeid));
				// p($attrData);
				//取出时间,类型
				$cityData = array();
				$typeData = array();
				$supportData = array();
				//循环重组数组
				foreach ($attrData as $k => $v) {
					// 取出时间
					if($v['is_type']==3){
						//字符转数组
						$v['attrvalue'] = explode(',', $v['attrvalue']);
						$cityData = $v['attrvalue'];
						
					}
					//取出类型
					if($v['is_type']==0){
						//字符转数组
						$v['attrvalue'] = explode(',', $v['attrvalue']);
						$typeData = $v['attrvalue'] ;
					}
					//取出支持
					if($v['is_type']==2){
						//字符转数组
						$v['attrvalue'] = explode(',', $v['attrvalue']);
						$supportData = $v['attrvalue'];
					}
				}
				//分配数据
				
				$this->assign('cityData',$cityData);
				$this->assign('typeData',$typeData);
//========================================商品数据++++++++++++++++++++++++++++++++++++
	        	$allCtaeData = K('Goods')->get_all(array('cateid'=>Q('get.cateid')),'goodsid desc',array('goodsid','title','cinemaid','startime','overtime','goodsname'));
		         
		         //商品数据---
		         $goodsid=array();
		        foreach ($allCtaeData as $key => $value) 
		        {
		          $goodsid[]=$value['goodsid'];
		        }
		        $cinemaid = array();
		        foreach ($allCtaeData as $key => $value) {
		        	$cinemaid[] =$value['cinemaid'];
		        }
		        //时间数据
		        // $timeData = K('Goodstime')->get_in(array('goodsid'=>$goodsid),'goodsid desc',array('timeid','timename','goodsid'));
		        //查出
		        // p($timeData);
		    
		        //DED详细描述---
		         $desData = K('Goodsdes')->get_in(array('goodsid'=>$goodsid),'goodsid desc',array('goodsimage'));
		         // p($desData);
		         // 数组的组合---图片路径/地点
		        foreach ($allCtaeData as $k => $v) {
		          // p($desData[$k]['goodsimage']);
		          $allCtaeData[$k]['goodsimage'] = $desData[$k]['goodsimage'];
		          $place = K('Cinema')->get_one(array('cinemaid'=>$v['cinemaid']));
		          // p($place);
		          $allCtaeData[$k]['cinemaname'] = $place['cinemaname'];
		          $allCtaeData[$k]['cinemaaddress'] = $place['cinemaaddress'];
		           $city = k('Goodsattr')->get_one(array('goodsid'=>$v['goodsid']),array('city','goodsid'));
	   		 // p($city);
	          $allCtaeData[$k]['city'] = $city['city'];
		      }
		      //******************GET传参数判断《子级导航》****************
	    	  $_GET['city'] = isset($_GET['city'])?$_GET['city']:NULL;
	    	  $temarr = array();
	    	  if($_GET['city'])
	    	  {
	    	  	foreach ($allCtaeData as $k => $v) {
	    	  		if($v['city']==$_GET['city']){
	    	  			$temarr[] = $allCtaeData[$k];
	    	  		}
	    	  	}
	    	  }

	        // p($allCtaeData);
	        $this->assign('allCtaeData',$allCtaeData);
		}
//******************************分页处理**************************************************
			//获得分页的数据
			// 定义超全局变量$countpage
			// global $countpage;
			// $pagesData = page_array(5,1,$allCtaeData,0);
			// p($countpage);
			// p($pagesData);
			 // $page = show_array($countpage,'List/inde');
			 // p($page);
			 // $this->assign('pagesData',$pagesData);
			  // $this->assign('page',$page);
		$this->display('list.html');	
	}
	/**
	 * [get_choose GET筛选]
	 * @return [type] [description]
	 */
	public function get_choose()
	{
		
				//筛选条件类型管理
				// 查出所有的分类，再根据传入的CATEID判断CLASS以及是否为子类
				$typeData  = K('Category')->get_all(array('pid'=>0),array('catename','cateid'));
				// 分配数据
				// 根据传入的CATEID查出子类ID
				$soncate = K('Category')->get_all(array('pid'=>Q('get.cateid')),array('catename','cateid'));
				$this->assign('typeData',$typeData);
				$this->assign('soncate',$soncate);
				// p($soncate);
	}


}